﻿namespace SharpF2.Components
{
	using System;
	using System.ComponentModel;
	using System.Windows.Controls;

	public partial class ListViewExtended<ItemType>
	{
		#region Public Members

		public static void RefreshListViewColumnWidths(GridView gv)
		{
			foreach (var gvc in gv.Columns)
			{
				gvc.Width = gvc.ActualWidth;
				gvc.Width = Double.NaN;
			}
		}

		#endregion

		#region Private Members

		private void ColumnWidthInitialize()
		{
			TypeDescriptor.GetProperties(typeof (ListViewExtended<ItemType>))["ItemsSource"].AddValueChanged(this,
			                                                                                                 ItemsSourceChanged);
		}

		private void ItemsSourceChanged(object sender, EventArgs e)
		{
			var source = ItemsSource as INotifyInnerDataChanged;
			if (null != source)
				source.InnerDataChanged += ItemsSourceInnerDataChanged;
		}

		private void ItemsSourceInnerDataChanged(object sender, EventArgs e)
		{
			var gridView = View as GridView;
			if (null == gridView) return;

			RefreshListViewColumnWidths(gridView);
		}

		#endregion
	}
}